package cn.six1943.deepchat.module.ai.agent;

import dev.langchain4j.model.chat.listener.ChatModelErrorContext;
import dev.langchain4j.model.chat.listener.ChatModelListener;
import dev.langchain4j.model.chat.listener.ChatModelRequestContext;
import dev.langchain4j.model.chat.listener.ChatModelResponseContext;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LogChatModelListener implements ChatModelListener {
    @Override
    public void onRequest(ChatModelRequestContext requestContext) {
        ChatRequest chatRequest = requestContext.chatRequest();

        log.info("agent::onRequest::message::{}", chatRequest.messages());
        log.info("agent::onRequest::parameter::{}", chatRequest.parameters());
        log.info("agent::onRequest::provider::{}", requestContext.modelProvider());
    }

    @Override
    public void onResponse(ChatModelResponseContext responseContext) {
        ChatResponse chatResponse = responseContext.chatResponse();

        log.info("agent::onResponse::message::{}", chatResponse.aiMessage());
        log.info("agent::onResponse::metadata::{}", chatResponse.metadata());
        log.info("agent::onResponse::provider::{}", responseContext.modelProvider());
    }

    @Override
    public void onError(ChatModelErrorContext errorContext) {
        Throwable error = errorContext.error();

        log.info("agent::onError::provider::{}", errorContext.modelProvider());
        error.printStackTrace();
    }
}
